from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.

def demo(request):
    img = request.POST.get("file")
    dis_path = "mxonline/media/course/"
    filename = gen_random_name()
    extension_name = ".png"
    dis_path_file = dis_path + filename + extension_name
    img_url = save_img_to_oss(img,dis_path_file)
    u_dict = {
        "status":"2345",
        "info":"chengle",
        "data":img_url,
    }
    return JsonResponse(u_dict)




def gen_random_name():
    return "123456789953426142"



'''
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created by victor
# Created Time: '2020/1/10 17:00'
LTAI4FtaF7kFdRf3D11BdFvu
58xvQ0kxwiyi1MRuBVRsjXG8Ic5niZ
'''


def up_file_to_alioss(ori_path, dis_file_path):
    '''
    用于上传图片到阿里oss存储空间中
    :param ori_path: 原始路径
    :param dis_file_path:
    :return:
    '''
    try:
        # -*- coding: utf-8 -*-
        import os
        import oss2
        # <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        # <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。

        # dis_path = "mxonline/media/course/"
        # ori_path = "C:/Users/Administrator/Desktop/edu/QQ截图20200107121030.png"
        # file_name = "python.png"

        # 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，
        # 请登录 https://ram.console.aliyun.com 创建RAM账号。
        auth = oss2.Auth('LTAI4FtaF7kFdRf3D11BdFvu', '58xvQ0kxwiyi1MRuBVRsjXG8Ic5niZ')
        # Endpoint以杭州为例，其它Region请按实际情况填写。

        bucket = oss2.Bucket(auth, 'http://oss-cn-beijing.aliyuncs.com', 'victor-fengming')

        base_url = "https://victor-fengming.oss-cn-beijing.aliyuncs.com/"

        img_url = base_url + dis_file_path
        # 必须以二进制的方式打开文件，因为需要知道文件包含的字节数。
        with open(ori_path, 'rb') as fileobj:
            # Seek方法用于指定从第1000个字节位置开始读写。上传时会从您指定的第1000个字节位置开始上传，直到文件结束。
            fileobj.seek(1000, os.SEEK_SET)
            # Tell方法用于返回当前位置。
            # current = fileobj.tell()
            bucket.put_object(dis_file_path, fileobj)
        print("文件路径->>", img_url)
        return img_url
    except:
        return ""


# 保存上传文件到aliyun
def upload_file_to_aliyun(PostFile, dis_file_path):
    '''
    用于上传图片到阿里oss存储空间中
    :param PostFile: 表单中的图片数据
    :param dis_file_path: 要保存的相对路径
    :return:
    '''
    access_key = "LTAI4FtaF7kFdRf3D11BdFvu"
    secret_key = "58xvQ0kxwiyi1MRuBVRsjXG8Ic5niZ"
    bucket_name = "victor-fengming"
    END_POINT = 'http://oss-cn-beijing.aliyuncs.com'
    try:
        import oss2
        auth = oss2.Auth(access_key, secret_key)
        bucket = oss2.Bucket(auth, END_POINT, bucket_name)
        # ret, info = put_file(token, key, upload_file)
        result = bucket.put_object(dis_file_path, PostFile)
        return True
        # if ret.get('key',None) == None:
        #  raise Exception('upload error')
        # else:
        #  return u"SUCCESS"
    except :
        return False


def save_img_to_oss(PostFile, dis_file_path):
    '''
    用于保存表单中的图片
    :param PostFile:
    :param dis_file_path:
    :return: img路径
    '''
    if upload_file_to_aliyun(PostFile, dis_file_path):
        # 要是成了
        base_url = "https://victor-fengming.oss-cn-beijing.aliyuncs.com/"
        img_url = base_url + dis_file_path
        return img_url
    else:
        # 要是没成返回空字符串
        return ""

if __name__ == '__main__':
    pass
